
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>VUEX规范 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="ajax.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="directory.html">
            
                <a href="directory.html">
            
                    
                    目录规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="ajax.html">
            
                <a href="ajax.html">
            
                    
                    ajax请求
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="vuex.html">
            
                <a href="vuex.html">
            
                    
                    VUEX规范
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >VUEX规范</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="vuex&#x4F7F;&#x7528;&#x89C4;&#x8303;">vuex&#x4F7F;&#x7528;&#x89C4;&#x8303;</h1>
<p>&#x4EE5;&#x524D;&#x5BF9;&#x4E8E;store&#x7684;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x5747;&#x653E;&#x5728;&#x540C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x5BF9;&#x4E8E;&#x5927;&#x578B;&#x9879;&#x76EE;&#xFF0C;&#x540D;&#x79F0;&#x5BB9;&#x6613;&#x91CD;&#x590D;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x5229;&#x4E8E;&#x540E;&#x671F;&#x7EF4;&#x62A4;</p>
<p>&#x73B0;&#x628A;vuex&#x96C6;&#x4E2D;&#x7BA1;&#x7406;&#x8D77;&#x6765;&#xFF0C;&#x5E76;&#x6839;&#x636E;&#x6A21;&#x5757;&#x6765;&#x533A;&#x5206;&#xFF0C;&#x6E05;&#x6670;&#x660E;&#x4E86;</p>
<h3 id="&#x5177;&#x4F53;&#x8BF4;&#x660E;">&#x5177;&#x4F53;&#x8BF4;&#x660E;</h3>
<p>&#x5728;&#x6BCF;&#x4E2A;&#x4E1A;&#x52A1;&#x6A21;&#x5757;&#x4E0B;&#xFF0C;&#x4F1A;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;<code>store</code>&#x7684;&#x76EE;&#x5F55;&#xFF0C;&#x8BE5;&#x76EE;&#x5F55;&#x4E0B;&#x5B58;&#x653E;&#x8FD9;&#x6240;&#x6709;vuex&#x7684;&#x72B6;&#x6001;
&#x5176;&#x4E2D;<code>index.js</code>&#x662F;api&#x603B;&#x5165;&#x53E3;&#xFF0C;&#x8D1F;&#x8D23;&#x5F15;&#x5165;&#x5176;&#x4ED6;&#x5B50;&#x6A21;&#x5757;&#x7684;store&#xFF0C;&#x5E76;&#x7EDF;&#x4E00;&#x5BF9;&#x5916;&#x63D0;&#x4F9B;store</p>
<p><strong>store&#x6587;&#x4EF6;&#x5939;&#x4E0B;&#x6709;&#x56DB;&#x4E2A;js <code>state.js</code>,<code>mutations.js</code>,<code>getters.js</code>,<code>actions.js</code>&#x5206;&#x522B;&#x6765;&#x5B58;&#x50A8;&#xFF0C;&#x4FEE;&#x6539;&#xFF0C;&#x83B7;&#x53D6;&#x8BE5;&#x4E1A;&#x52A1;&#x6A21;&#x5757;&#x516C;&#x7528;&#x7684;&#x72B6;&#x6001;</strong></p>
<h4 id="indexjs">index.js</h4>
<p><code>modules</code>&#x4E2D;&#x5F15;&#x5165;&#x5B50;&#x6A21;&#x5757;</p>
<p> <strong>&#x5F53;&#x4E3A;&#x5F00;&#x53D1;&#x6A21;&#x5F0F;&#x65F6;&#xFF0C;vuex&#x4F7F;&#x7528;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#xFF0C;&#x4FEE;&#x6539;state&#x53EA;&#x80FD;&#x901A;&#x8FC7;<code>mutations</code>&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Vue <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue&quot;</span>;
<span class="hljs-keyword">import</span> Vuex <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vuex&quot;</span>;
<span class="hljs-keyword">import</span> state <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./state&apos;</span>;
<span class="hljs-keyword">import</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./actions&apos;</span>;
<span class="hljs-keyword">import</span> getters <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./getters&apos;</span>;
<span class="hljs-keyword">import</span> mutations <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./mutations&apos;</span>;

<span class="hljs-keyword">import</span> login <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/login&apos;</span>

Vue.use(Vuex);

<span class="hljs-keyword">const</span> debug = process.env.NODE_ENV !== <span class="hljs-string">&apos;production&apos;</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> Vuex.Store({
  strict: debug,
  state,
  actions,
  getters,
  mutations,
  modules: {
    login
  },
});
</code></pre>
<h4 id="&#x516C;&#x7528;&#x7684;-mutationsjs">&#x516C;&#x7528;&#x7684; mutations.js</h4>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
  updateUserInfo(state, userInfo) {
    state.userInfo = userInfo;
  }
};
</code></pre>
<h4 id="loginjs-&#x5B50;&#x6A21;&#x5757;">login.js &#x5B50;&#x6A21;&#x5757;</h4>
<p> <strong><code>getters</code>&#x65B9;&#x6CD5;&#x76F8;&#x5F53;&#x4E8E;vue&#x7684;<code>computed</code>&#x4E0D;&#x53EF;&#x5BF9;state&#x8FDB;&#x884C;&#x4FEE;&#x6539;</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> storejs <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;storejs&apos;</span>
<span class="hljs-keyword">const</span> state = {
    access: {
        access_token: <span class="hljs-string">&quot;&quot;</span>
    },
    testMapState:<span class="hljs-string">&quot;&#x6211;&#x662F;mapState&quot;</span>
}

<span class="hljs-comment">// getters</span>
<span class="hljs-keyword">const</span> getters = {
    getToken: state =&gt; {
        <span class="hljs-keyword">return</span> state.access.access_token || storejs.get(<span class="hljs-string">&apos;token&apos;</span>);
    }
}

<span class="hljs-comment">// actions</span>
<span class="hljs-keyword">const</span> actions = {
    updateToken({ state, commit }, access_token) {
        <span class="hljs-keyword">if</span> (state.access.access_token != access_token) {
            commit(<span class="hljs-string">&apos;updateAccessToken&apos;</span>, access_token)
            storejs.set(<span class="hljs-string">&apos;token&apos;</span>, access_token)
        }
    }
}

<span class="hljs-comment">// mutations</span>
<span class="hljs-keyword">const</span> mutations = {
    updateAccessToken(state, access_token) {
        state.access.access_token = access_token
    },
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    namespaced: <span class="hljs-literal">true</span>,
    state,
    getters,
    actions,
    mutations
}
</code></pre>
<h4 id="&#x4F7F;&#x7528;vuex">&#x4F7F;&#x7528;vuex</h4>
<p>&#x901A;&#x8FC7;<code>mapGetters, mapMutations,mapActions,mapState</code>&#x6765;&#x8C03;&#x7528;vuex&#x7684; <code>getter, mutation,action,state</code>,&#x5BF9;&#x4E8E;&#x6CA1;&#x6709;<code>getter</code>&#x65B9;&#x6CD5;&#x7684;state&#x503C;&#xFF0C;&#x4F7F;&#x7528;mapState&#xFF0C;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><strong>js</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { mapGetters, mapMutations,mapActions,mapState  } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vuex&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
 computed:{
    ...mapGetters([<span class="hljs-string">&apos;userInfo&apos;</span>]),
    ...mapGetters(<span class="hljs-string">&apos;login&apos;</span>, {
      accessToken: <span class="hljs-string">&apos;getToken&apos;</span>
    }),
    ...mapState({
      testMapState: state =&gt; state.login.testMapState
    })
  },
  methods:{
    ...mapMutations(<span class="hljs-string">&apos;login&apos;</span>, [<span class="hljs-string">&apos;updateAccessToken&apos;</span>]),
    ...mapMutations([<span class="hljs-string">&apos;updateUserInfo&apos;</span>]),
    ...mapActions(<span class="hljs-string">&apos;login&apos;</span>, [<span class="hljs-string">&apos;updateToken&apos;</span>]),
    updataUserTest(){
      <span class="hljs-keyword">this</span>.updateUserInfo({userName:<span class="hljs-string">&quot;liuxin&quot;</span>,userId:<span class="hljs-string">&apos;123456&apos;</span>})
      <span class="hljs-keyword">this</span>.updateToken(<span class="hljs-string">&quot;fdkfjdskfjsdkffjksdfjd&quot;</span>)
      <span class="hljs-keyword">this</span>.updateAccessToken(<span class="hljs-string">&quot;fdkfjdskfjsdkffjksdfjd&quot;</span>)
    }
}
</code></pre>
<p><strong>html</strong></p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      userInfo:{{JSON.stringify(this.userInfo)}}<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
      AccessToken:{{this.accessToken}}<span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
      testMapState:{{this.testMapState}}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p><em>&#x4F7F;&#x7528;<code>mapGetters, mapMutations,mapActions,mapState</code>&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x5BF9;&#x4E8E;&#x6CA1;&#x6709;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x7684;&#x76F4;&#x63A5;&#x83B7;&#x53D6;&#x516C;&#x7528;&#x7684;&#xFF0C;&#x5BF9;&#x4E8E;&#x6709;&#x6A21;&#x5757;&#x540D;&#x79F0;&#x7684;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x6A21;&#x5757;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x65B9;&#x6CD5;&#x540D;&#x79F0;</em></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="ajax.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: ajax请求">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"VUEX规范","level":"1.5","depth":1,"previous":{"title":"ajax请求","level":"1.4","depth":1,"path":"ajax.md","ref":"ajax.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"vuex.md","mtime":"2020-03-19T05:20:04.604Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-19T06:12:42.684Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

